package com.example.toolbar.toolbar;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.example.toolbar.R;
import com.example.toolbar.databinding.ActivityToolbar2Binding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class Toolbar2Activity extends AppCompatActivity {

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        ActivityToolbar2Binding binding = ActivityToolbar2Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mToolbar = binding.tbar;
        binding.tbarTitle.setVisibility(View.VISIBLE);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.setNavigationIcon(R.mipmap.notification_icon);//设置导航栏
            mToolbar.setTitle("首页");
            mToolbar.setTitleTextColor(Color.parseColor("#f5f5f5"));
            mToolbar.setBackgroundColor(Color.parseColor("#0176da"));
            //可以设置溢出菜单的图标
//            toolbar.setOverflowIcon(getResources().getDrawable(R.mipmap.ic_menu_more_overflow));
            //添加menu
//            toolbar.inflateMenu(R.menu.base_toolbar_menu);
        }
        //如果setNavigationOnClickListener和onOptionsItemSelected中同时写了点击，那么setNavigationOnClickListener中的有效
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Toolbar2Activity.this, "通过toolbar的setNavigationOnClickListener方法弹出的", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.base_toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_item1:
                Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_item2:
                Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
                return true;
            case android.R.id.home:
                Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}